/*
Copyright (c) 2010 julien barbay <barbay.julien@gmail.com>

 Permission is hereby granted, free of charge, to any person
 obtaining a copy of this software and associated documentation
 files (the "Software"), to deal in the Software without
 restriction, including without limitation the rights to use,
 copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the
 Software is furnished to do so, subject to the following
 conditions:

 The above copyright notice and this permission notice shall be
 included in all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
*/

//TODO : test it
package martian.m4gic.services
{
	import flash.display.*;
	import flash.events.*;
	import flash.geom.Point;
	import flash.ui.Mouse;
	
	public class Cursors extends Service
	{
		public function Cursors() { super(); }
		
		/* service name */
		static private const NAME:String = "Cursors";
		
		/* service properties */
		static public const VISIBLE:String = "visible";
		static public const HIDDEN:String = "hidden";
		
		static private var current:*;
		static private var offset:Point;
		static private var s:String;
			static public function get status():String { return s; }
		
		/* service initialization */
		static private function _register(args:Object = null):void{}
		static private function _release(args:Object = null):void {}
		static private function _activate(args:Object = null):void
		{
			s = HIDDEN;
		}
		
		static private function _desactivate(args:Object = null):void
		{
			if (s != HIDDEN)
			{	
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, follow);
				stage.removeEventListener(Event.ACTIVATE, mOver);
				stage.removeEventListener(Event.DEACTIVATE, mOut);
				stage.removeChild(current);
				current = null;
				Mouse.show();
				
				s = HIDDEN;
			}
		}
		
		/* service methods */
		static public function show(cursor:DisplayObject, offset:Point = null, only:Boolean = true):void
		{
			if (available)
			{
				if (current != null) { stage.removeChild(current); }
				
				stage.addEventListener(MouseEvent.MOUSE_DOWN, follow);
				stage.addEventListener(MouseEvent.MOUSE_MOVE, follow);
				
				stage.addEventListener(Event.ACTIVATE, mOver);
				stage.addEventListener(Event.DEACTIVATE, mOut);

				Cursors.offset = (offset == null) ? new Point() : offset;
				
				current = cursor;
				current.mouseEnabled = current.mouseChildren = false;
				stage.addChild(current);
				
				follow(null);
				if (only) { Mouse.hide(); }
				
				s = VISIBLE;
			}
		}
		
		static public function hide():void
		{
			if (available)
			{
				stage.removeEventListener(MouseEvent.MOUSE_DOWN, follow);
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, follow);
				
				stage.removeEventListener(Event.ACTIVATE, mOver);
				stage.removeEventListener(Event.DEACTIVATE, mOut);
				
				stage.removeChild(current);
				current = null;
				Mouse.show();
				
				s = HIDDEN;
			}
		}
		
		static private function follow(e:MouseEvent):void
		{
			stage.setChildIndex(current, stage.numChildren - 1);
			
			current.x = stage.mouseX + offset.x;
			current.y = stage.mouseY + offset.y;
		}
		
		static private function mOver(e:*):void { current.visible = true; }
		static private function mOut(e:*):void { current.visible = false; }
		
		/* inheritance */
		static private var reference:Sprite;
		static private var dispatch:EventDispatcher;
		static private var stage:Stage;
		
		static private var registered:Boolean = false;
		static private var activated:Boolean = false;
			static public function get available():Boolean { return (registered && activated); }
		
		static public function register(container:Sprite, args:Object = null):void
		{
			if (!registered)
			{
				reference = container;
				dispatch = new EventDispatcher();
				stage = reference.stage;
				
				_register(args);
				
				registered = true;
				
				if (args != null) { if (args.active != undefined) { if (args.active == true) { activate(); } } }
			}
			else { trace("6:", NAME, "already registered."); }
		}
		
		static public function release(args:Object = null):void
		{
			if (registered && !activated)
			{
				stage = null;
				dispatch = null;
				reference = null;
				
				_release(args);
				
				registered = false;
			}
			else { trace("6:", NAME, "already released."); }
		}
		
		static public function activate(args:Object = null):void
		{
			if (registered && !activated)
			{
				activated = true;
				_activate(args);
			}			
		}
		
		static public function desactivate(args:Object = null):void
		{
			if (registered && activated)
			{
				activated = false;
				_desactivate(args);
			}
		}
		
		static private function dispatchEvent(event:Event):void
		{
			dispatch.dispatchEvent(event);
		}
		
		static public function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
		{
			dispatch.addEventListener(type, listener, useCapture, priority, useWeakReference);
		}
		
		static public function removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
		{
			dispatch.removeEventListener(type, listener, useCapture);
		}
	}
}